package com.automannn.atm_back.codegenerate.generate;

import com.automannn.atm_back.codegenerate.util.CodeResourceUtil;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 默认代码工厂
 * @author chenkh
 * @time 2020/6/27 15:33
 */
public class DefaultCodeFactory extends BaseCodeFactory{
    private GenerateContext l;
    private String m;

    public DefaultCodeFactory() {
    }

    public void a(String templateFileName, String type, Map data) throws TemplateException, IOException {
        try {
            String entityPackage = data.get("entityPackage").toString();
            String entityName = data.get("entityName").toString();
            String fileNamePath = this.b(type, entityPackage, entityName);
            String fileDir = StringUtils.substringBeforeLast(fileNamePath, "/");
            Template template = this.getConfiguration().getTemplate(templateFileName);
            FileUtils.forceMkdir(new File(fileDir + "/"));
            Writer out = new OutputStreamWriter(new FileOutputStream(fileNamePath), CodeResourceUtil.SYSTEM_ENCODING);
            template.process(data, out);
            out.close();
        } catch (TemplateException var10) {
            var10.printStackTrace();
            throw var10;
        } catch (IOException var11) {
            var11.printStackTrace();
            throw var11;
        }
    }

    public void b(String templateFileName, String type, Map data) throws TemplateException, IOException {
        try {
            String entityPackage = data.get("entityPackage").toString();
            String entityName = data.get("entityName").toString();
            String fileNamePath = this.b(type, entityPackage, entityName);
            String fileDir = StringUtils.substringBeforeLast(fileNamePath, "/");
            Template template = this.h().getTemplate(templateFileName);
            FileUtils.forceMkdir(new File(fileDir + "/"));
            Writer out = new OutputStreamWriter(new FileOutputStream(fileNamePath), CodeResourceUtil.SYSTEM_ENCODING);
            template.process(data, out);
            out.close();
        } catch (TemplateException var10) {
            var10.printStackTrace();
            throw var10;
        } catch (IOException var11) {
            var11.printStackTrace();
            throw var11;
        }
    }

    public String getProjectPath() {
        return this.m;
    }

    public String getClassPath() {
        String path = this.getClass().getResource("/").getPath();
        return path;
    }

    public String getTemplatePath() {
        String path = this.getClassPath() + CodeResourceUtil.TEMPLATEPATH;
        return path;
    }

    public String b(String type, String entityPackage, String entityName) {
        String path = this.getProjectPath();
        String codePath = "";
        if (this.A != null && CodeResourceUtil.PACKAGE_SERVICE_STYLE.equals(this.A)) {
            codePath = this.a(path, type, entityPackage, entityName);
        } else {
            codePath = this.b(path, type, entityPackage, entityName);
        }

        return codePath;
    }

    public void b(String templateFileName, String type) throws TemplateException, IOException {
        new HashMap();
        Map<String, Object> data = this.l.getContext();
        this.a(templateFileName, type, data);
    }

    public void c(String templateFileName, String type) throws TemplateException, IOException {
        new HashMap();
        Map<String, Object> data = this.l.getContext();
        this.b(templateFileName, type, data);
    }

    public void d(String html, String type) throws TemplateException, IOException {
        new HashMap();
        Map data = this.l.getContext();

        try {
            String entityPackage = data.get("entityPackage").toString();
            String entityName = data.get("entityName").toString();
            String fileNamePath = this.b(type, entityPackage, entityName);
            String fileDir = StringUtils.substringBeforeLast(fileNamePath, "/");
            FileUtils.forceMkdir(new File(fileDir + "/"));
            Configuration cfg = new Configuration();
            StringTemplateLoader loader = new StringTemplateLoader();
            loader.putTemplate(entityName, html);
            cfg.setTemplateLoader(loader);
            cfg.setDefaultEncoding("UTF-8");
            Template template = cfg.getTemplate(entityName);
            FileUtils.forceMkdir(new File(fileDir + "/"));
            Writer out = new OutputStreamWriter(new FileOutputStream(fileNamePath), CodeResourceUtil.SYSTEM_ENCODING);
            template.process(data, out);
            out.close();
        } catch (IOException var12) {
            var12.printStackTrace();
            throw var12;
        }
    }

    public GenerateContext d() {
        return this.l;
    }

    public void a(GenerateContext callBack) {
        this.l = callBack;
    }

    public void setProjectPath(String projectPath) {
        this.m = projectPath;
    }
}
